package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Banner;
import com.baizhi.springboot.entity.Role;
import com.baizhi.springboot.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/selectRoleByPage")
    @ResponseBody
    public Map selectRoleByPage(Integer page,Integer limit) {
        return roleService.selectRoleByPage(page, limit);
    }

    @RequestMapping("/insertRole")
    @ResponseBody
    //@RequiresRoles("superadmin")
    public Map  insertRole(Role role) throws IOException {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            roleService.insertRole(role);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteRole")
    @ResponseBody
    @RequiresRoles("superadmin")
    public Map  deleteRole(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            roleService.deleteRole(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneRole")
    @ResponseBody
    public Map selectOneRole(Integer id, HttpSession session) {
        Role role = roleService.selectOneRole(id);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("role", role);
        return map;
    }

    @RequestMapping("/updateRole")
    @ResponseBody
    @RequiresRoles("superadmin")
    public Map updateRole(Role role) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            roleService.updateRole(role);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

}
